package com.kd.juhaowan.ui.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.CommonApi;
import com.kd.juhaowan.http.api.GameListApi;
import com.kd.juhaowan.http.model.GameModel;
import com.kd.juhaowan.http.model.HomeResult;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.ui.GameDetailActivity;
import com.kd.juhaowan.ui.GameDetailActivity2;
import com.kd.juhaowan.ui.InviteActivity;
import com.kd.juhaowan.ui.MoreGiftActivity;
import com.kd.juhaowan.ui.PlatFormCoinActivity;
import com.kd.juhaowan.ui.SigninActivity;
import com.kd.juhaowan.ui.adapter.BannerPagerAdapter;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.holder.GameListViewHolder;
import com.kd.juhaowan.ui.holder.HotGameViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.kd.juhaowan.utils.LogUtil;
import com.kd.juhaowan.view.ScollTextView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnBannerListener;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragmentNew extends SmartFreshBaseFragment implements OnItemClickListener {

    @BindView(R.id.view_refreshlist_list)
    RecyclerView mRecyclerView;

    private BaseRecyclerViewAdapter<GameListViewHolder, GameModel> mAdapter;
    private HomeResult headModel;

    @BindView(R.id.mall_home_banner)
    Banner<String, BannerPagerAdapter> bannerView;

    @BindView(R.id.fragment_home_scrollTextView)
    ScollTextView mScrollTextView;

    @BindView(R.id.home_newgame_list)
    RecyclerView newGameListView;
    @BindView(R.id.view_refreshlist_refresh)
    SmartRefreshLayout refreshLayout;

    private BaseRecyclerViewAdapter<HotGameViewHolder, GameModel> mNewAdapter;
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.fragment_home_new;
    }

    @Override
    protected SmartRefreshLayout getSmartRefreshLayout() {
        return refreshLayout;
    }

    @Override
    protected String getSmartRefreshTaskTag() {
        return null;
    }

    @Override
    protected void onRequestNextPageData(int page) {
        initPageDate(page);
    }

    @Override
    protected boolean isNullData(HttpData response) {
        return false;
    }

    @Override
    protected void onButterKnifeCreateViewAfter() {
        super.onButterKnifeCreateViewAfter();
        bannerView.setBannerRound2(UIUtil.dip2px(getContext(), 5));
        bannerView.setIndicator(new RectangleIndicator(getContext()));
//        EasyHttp.post(this).api(new CommonApi("api/banner")).request(new HttpCallbackProxy<HttpListData2<HomeResult>>(this){
//            @Override
//            public void onHttpSuccess(HttpListData2<HomeResult> result, boolean cache) {
//                super.onHttpSuccess(result, cache);
//
////                        LogUtil.debugLog(result.getData().getAccess_token());
////                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
////                bannerModels = result.getData();
////                initPageDate(1);
//                initData();
//            }
//        });
        setAdapter();
        initData();
        initPageDate(1);
    }

    @Override
    protected void reInitDate() {
        initData();
        initPageDate(1);
    }

    private static final int ITEM_VIEW_TYPE_Title = 1;
    private static final int ITEM_VIEW_TYPE_Info = 2;
    private void setAdapter(){
        mNewAdapter = new BaseRecyclerViewAdapter<HotGameViewHolder, GameModel>() {
            @NonNull
            @Override
            public HotGameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_home_newgame, null, false);
                HotGameViewHolder holder=new HotGameViewHolder(view);
                holder.setOnItemClickListener(getOnItemClickListener());
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull HotGameViewHolder holder, int position) {
                holder.setDate(getItem(position));

            }
        };
        mNewAdapter.setOnItemClickListener(this);
        mNewAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);

        newGameListView.setLayoutManager(layoutManager);
        newGameListView.setAdapter(mNewAdapter);


        mAdapter = new BaseRecyclerViewAdapter<GameListViewHolder, GameModel>() {
            @NonNull
            @Override
            public GameListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                GameListViewHolder holder;
//                if(ITEM_VIEW_TYPE_Title==viewType){
//                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
//                    View view = inflater.inflate(R.layout.item_home_head, null, false);
//                    holder = new HomeHeadViewHolder(view);
//                    holder.setOnItemClickListener(getOnItemClickListener());
//                }else {
                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                    View view = inflater.inflate(R.layout.item_home_content, null, false);
                    holder = new GameListViewHolder(view);
                    holder.setOnItemClickListener(getOnItemClickListener());
//                }
                return holder;
            }

            @Override
            public void onBindViewHolder(@NonNull GameListViewHolder holder, int position) {
                holder.setDate(getItem(position));
            }
        };

        mAdapter.setOnItemClickListener(this);
        mAdapter.resetAdapterData(null);
//        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getContext());
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        layoutManager1.setOrientation(RecyclerView.VERTICAL);

        mRecyclerView.setLayoutManager(layoutManager1);
        mRecyclerView.setAdapter(mAdapter);
    }


    private int currentPage;
    private void initPageDate(int page){
        currentPage=page;
        EasyHttp.post(this).api(new GameListApi("0",page,"")).request(new HttpCallbackProxy<HttpListData<GameModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<GameModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
//                        LogUtil.debugLog(result.getData().getAccess_token());
//                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
//                bannerModels = result.getData();
                LogUtil.debugLog("gameTagFragment",page+"..");
                if(page==1){
                    LogUtil.debugLog("gameTagFragment",(headModel ==null)+".."+result.getData().getList().size());
//                    List<GameModel> models = new ArrayList<>();
//                    models.addAll(result.getData().getList());
//                    models.addAll(result.getData().getList());
//                    models.addAll(result.getData().getList());
//                    models.addAll(result.getData().getList());
//                    models.addAll(result.getData().getList());
//                    models.addAll(result.getData().getList());
//                    mAdapter.resetAdapterData(models);
                    mAdapter.resetAdapterData(result.getData().getList());
                }else {

                    mAdapter.appendAdapterData(result.getData().getList());
                }
//                finishRefreshOrLoadMore(true,false);
            }
        });
    }

    private void initData(){
        EasyHttp.post(this).api(new CommonApi("api/index")).request(new HttpCallbackProxy<HttpData<HomeResult>>(this){
            @Override
            public void onHttpSuccess(HttpData<HomeResult> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                headModel = result.getData();
//                bannerModels = models.getHot_game();
                BannerPagerAdapter bannerAdapter = new BannerPagerAdapter(headModel.getHot_game());

                bannerView.setIndicatorGravity(IndicatorConfig.Direction.RIGHT);
                bannerView.setAdapter(bannerAdapter);
                bannerView.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(Object data, int position) {
                        GameModel model = bannerAdapter.getData(position);
                        Intent intent= new Intent(getActivity(), GameDetailActivity.class);
                        intent.putExtra("id",model.getId());
                        startActivity(intent);
                    }
                });

                List<GameModel> temp = headModel.getNotice_list();
                if(temp!=null&&temp.size()>0){
                    mScrollTextView.setData(temp);

                }
                mNewAdapter.resetAdapterData(headModel.getNewGameList());
//                        LogUtil.debugLog(result.getData().getAccess_token());
//                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
//                bannerModels = result.getData();
////                initPageDate(1);
//                onRefresh(getSmartRefreshLayout());
            }
        });
    }


    @OnClick(R.id.home_menu2)
    public void gotoPingtai(View view){
        startActivity(new Intent(getActivity(), PlatFormCoinActivity.class));
    }
    @OnClick(R.id.home_menu1)
    public void goto648(View view){
        startActivity(new Intent(getActivity(), MoreGiftActivity.class));
    }
    @OnClick(R.id.home_menu3)
    public void gotoDjq(View view){
        startActivity(new Intent(getActivity(), InviteActivity.class));
    }
    @OnClick(R.id.home_menu4)
    public void gotoSignin(View view){
        startActivity(new Intent(getActivity(), SigninActivity.class));
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
        Intent intent= new Intent(getActivity(), GameDetailActivity.class);
            if(viewHolder instanceof  GameListViewHolder){
                GameModel model =mAdapter.getItem(position);
                intent.putExtra("id",model.getId());

            }else if (viewHolder instanceof  HotGameViewHolder){
                GameModel model = mNewAdapter.getItem(position);
                intent.putExtra("id",model.getId());
            }
            startActivity(intent);
    }
}
